package com.lagou.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;

@Component
public class TXManager {
    @Autowired
    private ConnectionUtils connectionUtil;

    /*开启事务*/
    public void startTransaction() throws Exception{
        Connection conn = connectionUtil.getCurrentThreadConnection();
        conn.setAutoCommit(false);//手动开启事务
    }

    /*提交事务*/
    public void commit() throws Exception{
        Connection conn = connectionUtil.getCurrentThreadConnection();
        conn.commit();
    }

    /*回滚事务*/
    public void rollback() throws Exception{
        Connection conn = connectionUtil.getCurrentThreadConnection();
        conn.rollback();
    }

    /*释放连接资源*/
    public void release() throws Exception{
        Connection conn = connectionUtil.getCurrentThreadConnection();
        conn.close();
        connectionUtil.release();//从当前线程上移除Connection对象
    }
}
